package com.ebelter.btcomlib.models.bluetooth.products.hrscale;


import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.bluetooth.bean.SendMessage;
import com.ebelter.btcomlib.utils.log.LogUtils;

import java.util.Calendar;


/**
 * byte制包工具类
 */
public class HRScaleBytesMakeFatory {

    private static final String TAG = "ScaleBytesMakeFatory";

    //心跳包字节
    private static final byte[] HEARTCHECKBYTES = {(byte) 0xab, 0x01, (byte) 0xb0};
    private static final String HEARTCHECK="心跳包";
    public static SendMessage getHEARTSmg(){//ok
        SendMessage heartMsg=new SendMessage();
        heartMsg.datas=HEARTCHECKBYTES;
        heartMsg.desc=HEARTCHECK;
        return heartMsg;
    }

    private static final String GetMesureDataACK="获取测量数据的回应";
    private static final byte[] getMesureDataACK= {(byte) 0xab, 0x02, (byte) 0xa3, 0x00};
    public SendMessage getMesureDataACK() {//ok
        SendMessage sendUserMsg=new SendMessage();
        sendUserMsg.datas=getMesureDataACK;
        sendUserMsg.desc=GetMesureDataACK;
        return sendUserMsg;
    }







    //接受到离线数据的一个回应ACK
    private static final byte[] getHistoryDataACK= {(byte) 0xab, 0x02, (byte) 0x9b, 0x01};
    private static final String GET_HISTORY_DATA_ACK="获取历史数据的回应";
    public SendMessage getetHistoryDataACK() {
        SendMessage sendUserMsg=new SendMessage();
        sendUserMsg.datas=getHistoryDataACK;
        sendUserMsg.desc=GET_HISTORY_DATA_ACK;
        return sendUserMsg;
    }



    private static final byte[] DeleteAllUserInfoByte = {(byte) 0xab, 0x02, (byte) 0xa1, 0x00};
    private static final String DeleteAllUserInfo="删除所有用户信息";
    public SendMessage deleteAllUserInfo() {
        SendMessage sendUserMsg=new SendMessage();
        sendUserMsg.datas=DeleteAllUserInfoByte;
        sendUserMsg.desc=DeleteAllUserInfo;
        return sendUserMsg;
    }




    //同步系统时间
    private byte[] getSystemClock(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int date = cal.get(Calendar.DATE);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        //周一至周日，1至7
        int week = cal.get(Calendar.DAY_OF_WEEK);
        week = (week == 1) ? 7 : (week - 1);

        int yearLowHex = year & 0xff;
        int yearHeightHex = (year >> 8) & 0xff;
        LogUtils.i(TAG,"year="+year+"--yearLowHex="+yearLowHex+"--yearHeightHex="+yearHeightHex+"--month="+month
        +"--date="+date+"--hour="+hour+"--minute="+minute+"--second="+second+"--week="+week);
        byte[] bytes = {
                (byte) 0xab,
                0x09,
                (byte) 0x98,
                (byte) yearLowHex,
                (byte) yearHeightHex,
                (byte) month,
                (byte) date,
                (byte) hour,
                (byte) minute,
                (byte) second,
                (byte) week

        };
        return bytes;
    }
    private static final String SystemClock="下发系统时间";
    public SendMessage getSyncSystemClock() {//ok
        SendMessage sendUserMsg=new SendMessage();
        sendUserMsg.datas=getSystemClock();
        sendUserMsg.desc=SystemClock;
        return sendUserMsg;
    }


    private static final String USERINFO = "下发用户信息";
    public SendMessage getUserInfoSmg(ScaleUser scaleUser){//ok
        SendMessage sendUserMsg = new SendMessage();
        sendUserMsg.datas=scaleUser.buildScaleUserData();
        sendUserMsg.desc=USERINFO;
        return sendUserMsg;
    }


    //OTA请求升级CRC
    private static final String OTAUPDATEREQUEST="OTA升级请求";
    /**
     * @param type 1蓝牙固件 2秤固件
     */
    public SendMessage getOTAUpdateRequest(int type, int pkgLen, byte cs) {
        int lenLowHex = pkgLen & 0xff;
        int lenHeightHex = (pkgLen >> 8) & 0xff;
        byte[] bytes = {
                (byte) 0xab,
                0x06,
                (byte) 0x9d,
                (byte) type,
                (byte) lenLowHex,
                (byte) lenHeightHex,
                cs,
                0
        };
        SendMessage sendUserMsg=new SendMessage();
        sendUserMsg.datas=bytes;
        sendUserMsg.desc=OTAUPDATEREQUEST;
        return sendUserMsg;
    }


    public static final byte[] getVersionByte = {(byte) 0xab, 0x01, (byte) 0x9c};
    public static final String getVersionStr = "获取版本信息";
    /**
     * 下发查询版本
     */
    public SendMessage getVersion() {//ok
        SendMessage sendUserMsg=new SendMessage();
        sendUserMsg.datas=getVersionByte;
        sendUserMsg.desc=getVersionStr;
        return sendUserMsg;
    }




    private static final String SendUpdateData="下发更新数据包";
    public SendMessage getSendUpdateData(byte[] bs, int pkgNo) {

        LogUtils.i(TAG,"准备下发更新数据包 bs.length"+bs.length+"---pkgNo="+pkgNo);

        int noLowHex = pkgNo & 0xff;//低八位
        int noHeightHex = (pkgNo >> 8) & 0xff;//高八位

        byte[] bytes = new byte[20];
        bytes[0] = (byte) 0xad;
        bytes[1] = (byte) 0x12;
        bytes[2] = (byte) noLowHex;
        bytes[3] = (byte) noHeightHex;


        for (int i = 4, j = 0; i < 20; i++, j++) {
            if (j >= bs.length) {
                bytes[i] = (byte) 0xff;
            } else {
                bytes[i] = bs[j];
            }
        }


        //后18位做xor，存入bytes[1]
        byte csXor = 0;
        for (int i = 2; i < bytes.length; i++) {
            csXor ^= bytes[i];
        }
        bytes[1] = csXor;

        //长度低字节再处理
        bytes[2] = (byte) (((pkgNo & 0xff) + 0x90) & 0xff);

        SendMessage sendUserMsg=new SendMessage();
        sendUserMsg.datas=bytes;
        sendUserMsg.desc=SendUpdateData;
        return sendUserMsg;


    }
}
